package com.jmhy.sdk.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.jmhy.sdk.activity.BaseFloatActivity;
import com.jmhy.sdk.activity.JmCommunityActivity;
import com.jmhy.sdk.activity.JmUserinfoActivity;
import com.jmhy.sdk.activity.View.FloatUserInfoView;
import com.jmhy.sdk.common.JiMiSDK;
import com.jmhy.sdk.config.AppConfig;
import com.jmhy.sdk.sdk.PayDataRequest;

import java.net.URISyntaxException;
import java.util.Timer;
import java.util.TimerTask;

public class FloatJsInterface {

    public final static String TAG = FloatJsInterface.class.getSimpleName();

    private Activity activity;
    private BaseFloatActivity baseFloatActivity;
    private Seference mSeference;

    private Boolean payResult;



    public void setPayResult(Boolean payResult) {
        this.payResult = payResult;
    }

    public Boolean isPayResult() {
        return payResult;
    }

    public FloatJsInterface(Activity activity, BaseFloatActivity baseFloatActivity) {
        this.activity = activity;
        this.mSeference = new Seference(activity);
        this.baseFloatActivity = baseFloatActivity;
    }

    @JavascriptInterface
    public void JavaScriptSavepassword(String user, String pwd, String uid) {
        this.mSeference.saveAccount(user, pwd, uid);
        AppConfig.saveMap(user, pwd, uid);
        Utils.saveUserToSd(this.activity);
    }

    @JavascriptInterface
    public void JavaScriptToJumpLogin() {
        if (this.baseFloatActivity != null) {
            this.baseFloatActivity.removeContentView();
        }
    }

    @JavascriptInterface
    public void closeWebView() {
        if (this.baseFloatActivity != null) {
            this.baseFloatActivity.removeContentView();
            this.baseFloatActivity.closeWebView();
        }
    }

    @JavascriptInterface
    public void JavaScriptback() {
        if (this.baseFloatActivity != null) {
            this.baseFloatActivity.removeContentView();
        }
    }

    @JavascriptInterface
    public void JavascriptToDown(String url) {
//        MediaUtils.saveImage(this.activity, MediaUtils.getBitmap(url));
        DialogUtils.showTip(this.activity, AppConfig.getString(this.activity, "float_snapshot_save"));
    }

    @JavascriptInterface
    public void JavaScriptToJumppassword() {
    }

    @JavascriptInterface
    public void JavaScriptModifyPassword(String pwd){


        String temUser = mSeference.getPreferenceData(
                Seference.ACCOUNT_FILE_NAME, Seference.ACCOUNT_1);

        String temUid = mSeference.getPreferenceData(
                Seference.ACCOUNT_FILE_NAME, Seference.UID_1);

        mSeference.saveAccount(temUser, pwd, temUid);
        AppConfig.saveMap(temUser, pwd, temUid);
        Utils.saveUserToSd(activity);


        Log.i("JavaSsword",pwd);
    }

    @JavascriptInterface
    public void JavaScriptGuestBindOnAccount(String account,String password){

        String temUid = mSeference.getPreferenceData(
                Seference.ACCOUNT_FILE_NAME, Seference.UID_1);

        mSeference.saveAccount(account, password, temUid);
        AppConfig.saveMap(account, password, temUid);
        Utils.saveUserToSd(activity);

        Log.i("account and password,",account + "--- password" + password);
    }

    @JavascriptInterface
    public void JavascriptSwitchUser() {
        Log.i("kk", "切换");


        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JiMiSDK.userlistenerinfo.onLogout("logout");
                JiMiSDK.switchAccount(activity);
                FloatJsInterface.this.baseFloatActivity.removeContentView();

                FloatUtils.destroyFloat();
            }
        });
    }

    @JavascriptInterface
    public void JavascriptSetAccount(final String oldAccount, final String newAccount, final String newPassword) {
        this.activity.runOnUiThread(new Runnable() {
            public void run() {
//                changeAccountUtil.changeAccount(FloatJsInterface.this.activity, true, oldAccount, newAccount, newPassword, "");
            }
        });
    }

    @JavascriptInterface
    public void JavascriptChangePassword(final String account, final String password) {
        this.activity.runOnUiThread(new Runnable() {
            public void run() {
//                changeAccountUtil.changeAccount(FloatJsInterface.this.activity, true, account, account, password, "");
            }
        });
    }

    @JavascriptInterface
    public void loadUrlByBrowser(String url) {
        this.activity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));
    }

    @JavascriptInterface
    public void payNotify(String result) {
        if(result.equals("1")){
            if(JiMiSDK.apiListenerInfo!=null){
                JiMiSDK.apiListenerInfo.onSuccess("success");
            }

            payResult = true;
            Log.i(TAG, "pay success");

            JiMiSDK.getStatisticsSDK().onPay(PayDataRequest.getmPayInfo(), PayDataRequest.getPayData(), JiMiSDK.payChannel, true);


        }else{
            if(JiMiSDK.apiListenerInfo!=null){
                JiMiSDK.apiListenerInfo.onSuccess("close");
            }

            payResult = false;
            Log.i(TAG, "pay failure");

            JiMiSDK.getStatisticsSDK().onPay(PayDataRequest.getmPayInfo(), PayDataRequest.getPayData(), JiMiSDK.payChannel, false);
        }

        FloatJsInterface.this.baseFloatActivity.removeContentView();

    }

    @JavascriptInterface
    public void closeAnimation() {
        Log.i("JsInterface", "closeAnimation");
        this.activity.runOnUiThread(new Runnable() {
            public void run() {
                if (FloatJsInterface.this.activity instanceof JmUserinfoActivity) {
                    ((JmUserinfoActivity) FloatJsInterface.this.activity).hiddenLoading();
                } else if (FloatJsInterface.this.activity instanceof JmCommunityActivity) {
                    ((JmCommunityActivity) FloatJsInterface.this.activity).hiddenLoading();
                }
            }
        });
    }

    @JavascriptInterface
    public void forceLogout(final String str) {
        Log.i("JsInterface", "forceLogout");
        this.activity.runOnUiThread(new Runnable() {
            public void run() {
                JiMiSDK.forceLogout(str);
            }
        });
    }

    @JavascriptInterface
    public void openWebView(String url, int type) {
        Intent intent = new Intent(this.activity, JmCommunityActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("type", type);
        this.activity.startActivity(intent);
    }


    public static boolean judgeStrNull(String str) {
        return (str == null || str.equals(""));
    }


 //**********************************1.4.3 新增
    @JavascriptInterface
    public void JavascriptCloseLoadingPage() {
        FloatJsInterface.this.baseFloatActivity.closeAnimation();
    }
    @JavascriptInterface
    public void JavascriptSaveQrCode(String qrUrl) {

        MediaUtils.saveImage(this.activity, MediaUtils.getBitmap(qrUrl));

//        DialogUtils.showTip(this.activity, AppConfig.getString(this.activity, "jm_float_snapshot_save"));

        Toast.makeText(activity, AppConfig.getString(this.activity, "jm_float_wx_snapshot_save"), Toast.LENGTH_LONG).show();



        new Timer().schedule(task, 2000);


    }

    private TimerTask task = new TimerTask() {
        @Override
        public void run() {
            openw0x0();
        }};

    @JavascriptInterface
    public void openw0x0(){
        try {

            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
            activity.startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();

            DialogUtils.showTip(this.activity,AppConfig.getString(this.activity, "jm_no_install_wechat"));
        }
    }

    @JavascriptInterface
    public void openq0q0(String url){
        Log.i("openq0q0",url);
        if (judgeStrNull(url)){
            return;
        }
        if (url.equals("mqqwpa://")) {
            try {

                Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
                activity.startActivity(intent);

            } catch (Exception e) {
                e.printStackTrace();
                DialogUtils.showTip(this.activity,AppConfig.getString(this.activity, "jm_no_install_qq"));
            }
        }else{
            Intent intent = null;
            try {
                intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                activity.startActivity(intent);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

        }


}

    @JavascriptInterface
    public void JavascriptOpenTurntable(String url){
        FloatJsInterface.this.baseFloatActivity.closeWebPageAndOpenTurntable(url);
    }

    @JavascriptInterface
    public void JavascriptReportAction(String action){
        JiMiSDK.reportUserAction(activity,action);
    }

    /**
     * 1.4.5新增js方法
     */
    @JavascriptInterface
    public void closeWebPageAndOpenUserCenter(String url) {
        FloatJsInterface.this.baseFloatActivity.closeWebPageAndOpenUserCenter(url);
    }

    /**
     * 1.4.7新增支付下发订单号js方法
     */
    @JavascriptInterface
    public void JavascriptPaymentOrder(String order) {
//        FloatJsInterface.this.baseFloatActivity.paymentOrder(order);

    }

}
